﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XCommon.Types;

namespace XWeb.Code
{

    internal class XNormalJson : XCommon.Code.DO<XNormalJson>
    {
        private string mtbGroupsJson;
        public string tbGroupsJson
        {
            get
            {
                if (string.IsNullOrEmpty(mtbGroupsJson))
                {
                    List<Ps> temp = new List<Ps>();
                    Ps item = new Ps();
                    item.Add("typeid", "1");
                    item.Add("typename", "安卓看友");
                    item.Add("itype", "1");
                    item.Add("iviewpower", "0");
                    temp.Add(item);
                    mtbGroupsJson = XCommon.LitJson.JsonMapper.ToJson(temp);
                }
                return mtbGroupsJson;
            }
        }

        private string mNotUpdatetbGroups;
        public string NotUpdatetbGroups
        {
            get
            {
                if (string.IsNullOrEmpty(mNotUpdatetbGroups))
                {
                    mNotUpdatetbGroups = "[{\"key\":\"ok\"}]";
                }
                return mNotUpdatetbGroups;
            }
        }
    }

    public class XDALManager : XCommon.Code.DO<XDALManager>
    {
        private TimeSpan _reGetToDayListSpan = new TimeSpan(0, 2, 0);

        private Dictionary<int, Dictionary<int, DateTime>> _allGroupListLastGetTime;

        public XDALManager()
        {
            _allGroupListLastGetTime = new Dictionary<int, Dictionary<int, DateTime>>();
        }

        /// <summary>
        /// 获取导航
        /// </summary>
        /// <returns></returns>
        public string GettbGroups()
        {
            string strJson = XCacheManager.It.GetJson(EJsonCacheKeys.tbGroups);
            if (string.IsNullOrEmpty(strJson))
            {
                strJson = XBALManage.It.GettbGroups();
            }
            return strJson;
        }

        public string GetGroupListData(int groupid, byte ipageindex)
        {
            string strJson = XCacheManager.It.GetPageJson(groupid, ipageindex);
            if (string.IsNullOrEmpty(strJson))
            {
                strJson = XBALManage.It.GetGroupListData(groupid, ipageindex);
            }
            return strJson;
        }

        public string GetPhotoItem(ushort itemid)
        {
            string strJson = XCacheManager.It.GetPhotoItemJson(itemid);
            if (string.IsNullOrEmpty(strJson))
            {
                strJson = XBALManage.It.GetPhotoItemByID(itemid);
            }
            return strJson;
        }

    }

    internal class XBALManage : XCommon.Code.DO<XBALManage>
    {
        public XBALManage()
        {
            GettbGroups();
        }

        public string GettbGroups()
        {
            List<Ps> ret = XDAL.tbGroups.It.Select();
            if (ret == null || ret.Count <= 0)
            {
                return XNormalJson.It.tbGroupsJson;
            }
            XCacheManager.It.TbGroupsCount = ret.Count;
            string strJson = XCommon.LitJson.JsonMapper.ToJson(ret);
            XCacheManager.It.SetJson(EJsonCacheKeys.tbGroups, strJson);
            return strJson;
        }

        public string GetGroupListData(int groupid, byte ipageindex)
        {
            List<Ps> retList = XDAL.tbPhotos.It.SelectGroupList(groupid, ipageindex);
            if (retList != null && retList.Count > 0)
            {
                string strJson = XCommon.LitJson.JsonMapper.ToJson(retList);
                XCacheManager.It.SetPageJson(groupid, ipageindex, strJson);
                return strJson;
            }
            return "";
        }

        public string GetPhotoItemByID(ushort itemid)
        {
            List<Ps>[] retList = XDAL.tbPhotos.It.SelectPhotoItemByID(itemid);
            if (retList != null)
            {
                string strJson = XCommon.LitJson.JsonMapper.ToJson(retList);
                XCacheManager.It.SetPhotoItemJson(itemid, strJson);
                return strJson;
            }
            return "";
        }
    }
}